﻿
#region Copyright & License
// --------------------------
// Copyright 2008 Daniel Gray
// --------------------------
// This file is part of Kale.
//
// Kale is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Kale is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with Kale.  If not, see <http://www.gnu.org/licenses/>.
#endregion
#region File details
// --------------------------------------------------------
// Filename          : ImageStoreNodeHelper.cs
// Author            : Daniel Gray
// Created           : 15/04/2008
// --------------------------------------------------------
#endregion
            
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kale.Core;
using System.Drawing;
using System.Web;

namespace Kale.Web.Services
{
   public static class ImageStoreNodeHelper
   {
      private static string Encode(string value)
      {
         return HttpUtility.UrlEncode(value);
      }

      private static string Decode(string encodedValue)
      {
         return HttpUtility.UrlDecode(encodedValue);
      }

      public static ImageStoreNode[] ConvertNodes(IImageNode[] nodes)
      {
         List<ImageStoreNode> convertedNodes = new List<ImageStoreNode>();

         foreach (IImageNode node in nodes)
         {
            convertedNodes.Add(ConvertNode(node));
         }

         return convertedNodes.ToArray();
      }

      public static ImageStoreNode ConvertNode(IImageNode node)
      {
         ImageStoreNode convertedNode = new ImageStoreNode(Encode(node.NodeId.ToString()), node.Name);

         convertedNode.Leaf = (node.NodeType == NodeType.File);

         return convertedNode;
      }

      public static ImageStoreNode[] GetChildren(string id)
      {
         return GetChildren(id, null);
      }

      public static ImageStoreNode[] GetChildFolders(string id)
      {
         return GetChildren(id, NodeType.Folder);
      }

      public static ImageStoreNode[] GetChildFiles(string id)
      {
         return GetChildren(id, NodeType.File);
      }

      private static ImageStoreNode[] GetChildren(string id, NodeType? nodeType)
      {
         IImageNode currentNode = RootNodeProvider.Instance.LoadNode(NodeId.ParseFromIdString(Decode(id)));

         if (nodeType.HasValue)
         {
            // select the children that are folders...
            return ConvertNodes(currentNode.Children.Where(c => c.NodeType == nodeType.Value).ToArray());
         }
         else
         {
            return ConvertNodes(currentNode.Children.ToArray());
         }
      }

      
   }
}
